package jee.boot.common.basic;

import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.common.basic.result.Result;
import jee.boot.common.properties.ProjectConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller基类
 *
 * @param <S>
 * @param <T>
 */
@Transactional
public abstract class CrudController<S extends CrudService, T extends DataEntity<T>> {
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    protected S service;
    @Autowired
    protected ProjectConfig projectConfig;

    /**
     * 查看
     *
     * @param entity
     * @return
     */
    @PostMapping("/get")
    public ResponseMessage<T> get(@RequestBody T entity) {
        T data = (T) service.get(entity);
        return Result.success(data);
    }

    /**
     * 列表
     *
     * @param entity
     * @return
     */
    @PostMapping("/list")
    public ResponseMessage<Page<T>> list(@RequestBody T entity) {
        if (entity.getPage() == null) {
            entity.setPage(new Page<>());
        }
        Page<T> findPage = service.findPage(entity);
        return Result.success(findPage);
    }

    /**
     * 所有数据列表,不分页
     *
     * @param entity
     * @return
     */
    @PostMapping("/all")
    public ResponseMessage<List<T>> all(@RequestBody T entity) {
        List<T> list = service.findList(entity);
        return Result.success(list);
    }
    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @PostMapping("/save")
    public ResponseMessage<T> save(@RequestBody T entity) {
        service.save(entity);
        return Result.success(entity);
    }

    /**
     * 批量新增
     *
     * @param list
     * @return
     */
    @PostMapping("/batch/save")
    public ResponseMessage<List<T>> batchSave(@RequestBody List<T> list) {
        service.batchSave(list);
        return Result.success(list);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @PostMapping("/update")
    public ResponseMessage<T> update(@RequestBody T entity) {
        service.update(entity);
        return Result.success(entity);
    }

    /**
     * 批量修改
     *
     * @param list
     * @return
     */
    @PostMapping("/batch/update")
    public ResponseMessage<List<T>> batchUpdate(@RequestBody List<T> list) {
        service.batchUpdate(list);
        return Result.success(list);
    }

    /**
     * 删除
     *
     * @param entity
     * @return
     */
    @PostMapping("/delete")
    public ResponseMessage<Object> delete(@RequestBody T entity) {
        service.delete(entity);
        return Result.success();
    }

    /**
     * 逻辑删除
     *
     * @param entity
     * @return
     */
    @PostMapping("/logic/delete")
    public ResponseMessage<Object> logicDelete(@RequestBody T entity) {
        service.logicDelete(entity);
        return Result.success();
    }
    /**
     * 批量删除
     *
     * @param entity
     * @return
     */
    @PostMapping("/batch/delete")
    public ResponseMessage<Object> batchDelete(@RequestBody T entity) {
        service.batchDelete(entity);
        return Result.success();
    }

    /**
     * 导出
     *
     * @param entity
     * @return
     */
    @PostMapping("/export")
    public ResponseMessage<Object> exportData(@RequestBody T entity, HttpServletResponse response) {
        service.exportData(entity, response);
        return Result.success();
    }

    /**
     * 导出
     *
     * @param entity
     * @return
     */
    @GetMapping("/export")
    public ResponseMessage<Object> exportDataGet(T entity, HttpServletResponse response) {
        service.exportData(entity, response);
        return Result.success();
    }

    /**
     * 导入模板
     *
     * @return
     */
    @PostMapping("/template")
    public void importTemplate(@RequestBody T entity, HttpServletResponse response) {
        service.importTemplate(entity, response);
    }

    /**
     * 导入模板
     *
     * @return
     */
    @GetMapping("/template")
    public void importTemplateGet(T entity, HttpServletResponse response) {
        service.importTemplate(entity, response);
    }

    /**
     * 导入
     *
     * @param file
     * @return
     */
    @PostMapping("/import")
    public ResponseMessage<Object> importData(MultipartFile file) {
        Object data = service.importData(file);
        return Result.success(data);
    }

}
